package com.webgis.demo.controll;


import com.webgis.demo.code.Result;
import com.webgis.demo.pojo.KeyWord;
import com.webgis.demo.pojo.Map;
import com.webgis.demo.pojo.Section;
import com.webgis.demo.pojo.SectionMap;
import com.webgis.demo.server.MapServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Objects;

@Controller
public class Mapcontroll {
    @Autowired
    MapServer mapServer;

    @CrossOrigin
    @GetMapping("/api/maps")
    @ResponseBody
    public List<Map> getmap() throws Exception {
        return mapServer.getMap();
    }
    @CrossOrigin
    @PostMapping(value = "api/addMap")
    @ResponseBody
    public Result addmap(@RequestBody Map requestMap) {
       return mapServer.addMapPoint(requestMap);

    }

    @CrossOrigin
    @PostMapping(value = "api/findSections")
    @ResponseBody
    public List<SectionMap> login(@RequestBody KeyWord request) {
        if(Objects.equals(request.getKeywords(), ""))
            return mapServer.getSection(request.getIndex());
        else
            return mapServer.getKeySection(request.getKeywords(),request.getIndex());
    }

    @CrossOrigin
    @GetMapping(value = "api/maxId")
    @ResponseBody
    public int findMaxId(){
        return mapServer.getMaxSectionId();
    }

    @CrossOrigin
    @PostMapping(value = "api/addSection")
    @ResponseBody
    public Result addSection(@RequestBody Section request){
        return mapServer.insertIntoSection(request);
    }

    @CrossOrigin
    @PostMapping(value = "api/sectionSum")
    @ResponseBody
    public int addSection(@RequestBody KeyWord res){
        if(Objects.equals(res.getKeywords(), ""))
            return mapServer.getSumSectionId();
        else
            return mapServer.getSumKeySection(res.getKeywords());

    }

    @CrossOrigin
    @PostMapping(value = "api/deleteSections")
    @ResponseBody
    public Result deleteSection(@RequestParam int key){
        mapServer.deleteSection(key);
        return new Result(200);
    }

}
